/*============================================================================*/

* Do: Merge Data 

/*============================================================================*/

* ==============================================================================
* SECTION: DIRECTORIES
* ==============================================================================

if 		"`c(username)'" == "INSERT YOUR USERNAME HERE" {
			global DIR "INSERT YOUR PATH HERE"					
		}


cd "$DIR"

global RAW		"$DIR"
global CLEAN	"$DIR"


* ==============================================================================
* SECTION: GENERATE MASTER FILE (BASELINE AND ENDLINE)
* ==============================================================================

*------------------------ MERGE BASELINE
use "$RAW/HH_baseline.dta", clear

merge m:1 agn_ea using "$RAW/AGN_baseline.dta"
	drop _merge

* RENAME ALL VARIABLES 
rename agn_* BLagn_*
rename brt_* BLbrt_*
rename kds_* BLvill_*

drop   BLvill_ea BLagn_ea  
rename BLbrt_id brt_id
rename BLbrt_ea agn_id
order brt_id agn_id district-nb_vill , first

tempfile master_baseline
save `master_baseline'

*------------------------ MERGE ENDLINE
use "$RAW/HH_endline.dta", clear

rename brt_ea agn_ea

merge m:1 agn_ea using "$RAW/AGN_endline.dta"

rename agn_ea brt_ea
drop _merge

rename agn_* ELagn_*
rename brt_* ELbrt_*

rename ELbrt_id brt_id
rename ELbrt_ea agn_id

order brt_id agn_id, first

tempfile master_endline
save `master_endline'

*------------------------ MERGE BASELINE+ENDLINE
use `master_baseline'

merge  1:1  brt_id agn_id using `master_endline'

* SAVE OBSERVATIONS THAT ARE NOT IN THE ENDLINE
gen attrition	= (_merge == 1) if _merge != 2
	label var attrition "HH not interviewed at endline"

gen add_clients = (_merge == 2)
	drop _merge

drop if add_clients == 1

destring agn_id, replace
destring brt_id, replace

tempfile master_all
save `master_all'

* ==============================================================================
* SECTION: MERGE WITH ADMINISTRATIVE DATA
* ==============================================================================

*------------------------ ALL TRANSACTIONS

use `master_all', clear

* CLEAN
tostring brt_id,replace
replace brt_id = "0" + "0" + brt_id 	if strlen(brt_id) == 7 
replace brt_id = "0" + brt_id 			if strlen(brt_id) == 8

**** NOTE: THIS DATASET IS NOT PUBLICLY AVAILABLE DUE TO CONFIDENTIALITY CONCERNS *****
**** WE HAVE COMMENTED OUT THIS MERGE TO ALLOW THE CODE TO RUN *****

*merge 1:1 brt_id using "$RAW/ecash_simakmur_transactions_id.dta"
*	drop if _merge == 2
*	drop _merge
rename agn_id agn_cl


save "$CLEAN/master_admin_all.dta", replace 

*------------------------ AT THE VILLAGE LEVEL
use `master_all', clear

* CLEAN
rename agn_id agn_cl
rename BLvill_id village_id
rename BLagn_id agn_id
gen agn_idsh =  substr(agn_id, 1, 6)
gen agn_idsh2 = substr(ELagn_id,1,6)
collapse (firstnm) distance noofcompetitors nb_hh BLvill* BLagn*  ELagn* ///
		 (max) wave dcode* high_private high_public private high public low ///
		 	   low_public low_private svar1-svar3 ///
		 (sum) ELbrt_ar09_* ELbrt_ar10_* ELbrt_ef16_*, by(agn_cl agn_id village_id)

* ADD ADMIN DATA
**** NOTE: THIS DATASET IS NOT PUBLICLY AVAILABLE DUE TO CONFIDENTIALITY CONCERNS *****
**** WE HAVE COMMENTED OUT THIS MERGE TO ALLOW THE CODE TO RUN *****

gen agn_ea = agn_cl
*merge 1:1 agn_ea using "$RAW/transactions_id_village_level_all.dta"
*	drop _merge

save "$CLEAN/admin_data_vill_all.dta", replace
